package com.ea.controller.back;

import cn.hutool.http.server.HttpServerRequest;
import com.ea.common.response.JsonResult;
import com.ea.service.UploadImgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author NAME-L
 * @Description TODO
 * @className UploadFileController
 * @date 2023-03-04 01:09:37
 */
@RestController
@CrossOrigin
@RequestMapping("/electric/back/upload")
public class UploadFileController {

    @Resource
    private UploadImgService uploadImgService;

    @PostMapping("/upload/img")
    public JsonResult uploadImgFile(@RequestParam("img") MultipartFile picture, HttpServerRequest request) {
        String originalFileName = uploadImgService.uploadImgFile(picture);
        if (StringUtils.isEmpty(originalFileName)) {
            return JsonResult.doFailed("上传失败");
        }
        return JsonResult.doSuccessful("上传成功" , originalFileName);
    }
}
